<!DOCTYPE html>
<html>
<head>
<title>Agent Equality Test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
// This test checks whether the JavaScript agent is created in a proper
// granularity. See:
// https://html.spec.whatwg.org/multipage/webappapis.html#integration-with-the-javascript-agent-formalism

// For details on each test, see the comments of the iframe file.
async_test(t => {
  let iframe = document.createElement('iframe');
  iframe.src =
    'http://127.0.0.1:8000/security/resources/agent-equality-same-origin.html';

  window.addEventListener(
    'message',
    t.step_func(evt => {
      if (evt.data[0] !== 'same-origin test') {
        return;
      }
      assert_equals(evt.data.length, 4);
      assert_equals(evt.data[1], evt.data[2]);
      assert_equals(evt.data[1], evt.data[3]);
      t.done();
    }));

  document.body.appendChild(iframe);
  t.add_cleanup(() => { document.body.removeChild(iframe); });
}, 'Same-origin documents must share the same agent.');

async_test(t => {
  let iframe = document.createElement('iframe');
  iframe.src =
    'http://127.0.0.1:8000/security/resources/agent-equality-cross-origin.html';

  window.addEventListener(
    'message',
    t.step_func(evt => {
      if (evt.data[0] !== 'cross-origin test') {
        return;
      }
      assert_equals(evt.data.length, 3);
      assert_not_equals(evt.data[1], evt.data[2]);
      t.done();
    }));

  document.body.appendChild(iframe);
  t.add_cleanup(() => { document.body.removeChild(iframe); });
}, 'Cross-origin documents should receive a different agent.');

async_test(t => {
  let iframe = document.createElement('iframe');
  iframe.src =
    'http://127.0.0.1:8000/security/resources/agent-equality-skip-level-same-origin.html';

  window.addEventListener(
    'message',
    t.step_func(evt => {
      if (evt.data[0] !== 'skip-level same-origin test') {
        return;
      }
      assert_equals(evt.data.length, 3);
      assert_equals(evt.data[1], evt.data[2]);
      t.done();
    }));

  document.body.appendChild(iframe);
  t.add_cleanup(() => { document.body.removeChild(iframe); });
}, 'Same-origin documents must share the same agent even if opened by cross-origin document.');

async_test(t => {
  let iframe = document.createElement('iframe');
  iframe.src =
    'http://127.0.0.1:8000/security/resources/agent-equality-same-origin-different-ports.html';

  window.addEventListener(
    'message',
    t.step_func(evt => {
      if (evt.data[0] !== 'same-origin different-ports test') {
        return;
      }
      assert_equals(evt.data.length, 3);
      assert_equals(evt.data[1], evt.data[2]);
      t.done();
    }));

  document.body.appendChild(iframe);
  t.add_cleanup(() => { document.body.removeChild(iframe); });
}, 'Documents on same-origin-but-different-ports should receive the same agent.');

async_test(t => {
  let iframe = document.createElement('iframe');
  iframe.src =
    'http://127.0.0.1:8000/security/resources/agent-equality-different-schemes.html';

  window.addEventListener(
    'message',
    t.step_func(evt => {
      if (evt.data[0] !== 'different-schemes test') {
        return;
      }
      assert_equals(evt.data.length, 3);
      assert_not_equals(evt.data[1], evt.data[2]);
      t.done();
    }));

  document.body.appendChild(iframe);
  t.add_cleanup(() => { document.body.removeChild(iframe); });
}, 'Documents with different schemes (HTTP and HTTPS) should receive different agents.');

async_test(t => {
  let iframe = document.createElement('iframe');
  iframe.src =
    'http://127.0.0.1:8000/security/resources/agent-equality-data-url.html';

  window.addEventListener(
    'message',
    t.step_func(evt => {
      if (evt.data[0] !== 'data scheme test') {
        return;
      }
      assert_equals(evt.data.length, 3);
      assert_not_equals(evt.data[1], evt.data[2]);
      t.done();
    }));

  document.body.appendChild(iframe);
  t.add_cleanup(() => { document.body.removeChild(iframe); });
}, 'Frame loaded as data: URL should receive a unique agent that is different from the parent\'s.');

async_test(t => {
  let iframe = document.createElement('iframe');
  iframe.src =
    'http://127.0.0.1:8000/security/resources/agent-equality-srcdoc.html';

  window.addEventListener(
    'message',
    t.step_func(evt => {
      if (evt.data[0] !== 'srcdoc iframe test') {
        return;
      }
      assert_equals(evt.data.length, 3);
      assert_equals(evt.data[1], evt.data[2]);
      t.done();
    }));

  document.body.appendChild(iframe);
  t.add_cleanup(() => { document.body.removeChild(iframe); });
}, 'srcdoc iframe should receive the same agent as the parent\'s.');

async_test(t => {
  let iframe = document.createElement('iframe');
  iframe.src =
    'http://127.0.0.1:8000/security/resources/agent-equality-after-detach.html';

  window.addEventListener(
    'message',
    t.step_func(evt => {
      if (evt.data[0] !== 'same-origin detach test') {
        return;
      }
      assert_equals(evt.data.length, 3);
      assert_equals(evt.data[1], evt.data[2]);
      t.done();
    }));

  document.body.appendChild(iframe);
  t.add_cleanup(() => { document.body.removeChild(iframe); });
}, 'Same-origin documents must share the same agent even after detaching.');

// TODO(yutak): Add tests that check agents after navigations.
</script>
</body>
</html>
